package com.xm.httpapi.BaseView;


import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.xm.httpapi.Aspect.ViewInjectUtils;
import com.xm.httpapi.BaseApi.ComObserver;
import com.xm.httpapi.BaseApi.ComTransformer;
import com.xm.httpapi.BaseApiInterface.OnLoadingListener;
import com.xm.httpapi.BaseApiInterface.ProgressManageError;

import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.disposables.CompositeDisposable;
/**
 * Author:guohaijin
 * Date:
 * Description:
 */
public abstract class BaseFragment extends Fragment {
    private CompositeDisposable mCompositeDisposable;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(ViewInjectUtils.injectLayout(getActivity()), container, false);
    }

    @Override
    public void onViewCreated(View v, Bundle savedInstanceState) {
        super.onViewCreated(v, savedInstanceState);
        initData(savedInstanceState);
        if (mCompositeDisposable == null) mCompositeDisposable = new CompositeDisposable();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mCompositeDisposable != null) {
            mCompositeDisposable.clear();
            mCompositeDisposable = null;
        }
    }

    protected abstract void initData(Bundle savedInstanceState);

    protected void intent(Class activity) {
        Intent intent = new Intent(getActivity(), activity);
        startActivity(intent);
    }

    protected void intent(Class activity, Map<String, String> map) {
        Intent intent = new Intent(getActivity(), activity);
        for (String key : map.keySet()) {
            intent.putExtra(key, map.get(key));
        }
        startActivity(intent);
    }

    protected void intent(Class activity, int classType) {
        Intent intent = new Intent(getActivity(), activity);
        startActivityForResult(intent, classType);
    }

    protected void intent(Class activity, int classType, Map<String, String> map) {
        Intent intent = new Intent(getActivity(), activity);
        for (String key : map.keySet()) {
            intent.putExtra(key, map.get(key));
        }
        startActivityForResult(intent, classType);
    }

    protected void subscribe(Observable observable, ComObserver comObserver) {
        observable.compose(new ComTransformer()).subscribe(comObserver);
    }

    /**
     * @显示默认loading的请求
     */
    protected ComObserver request(OnLoadingListener onLoadingListener) {
        return request(onLoadingListener, true);
    }

    /**
     * @自定义是否显示loading的请求
     */
    protected ComObserver request(OnLoadingListener onLoadingListener, boolean isLoading) {
        return new ComObserver(mCompositeDisposable, onLoadingListener, getContext(), isLoading);
    }

    /**
     * @自定义loading样式的请求
     */
    protected ComObserver request(OnLoadingListener onLoadingListener, AlertDialog alertDialog) {
        return new ComObserver(mCompositeDisposable, onLoadingListener, alertDialog, getContext(), true);
    }

    /**
     * @显示默认loading的请求并且请求带错误回调
     */
    protected ComObserver request(OnLoadingListener onLoadingListener, ProgressManageError progressManageError) {
        return request(onLoadingListener, progressManageError, true);
    }

    /**
     * @自定义是否显示loading的请求并且请求带错误回调
     */
    protected ComObserver request(OnLoadingListener onLoadingListener, ProgressManageError progressManageError, boolean isLoading) {
        return new ComObserver(mCompositeDisposable, onLoadingListener, progressManageError, getContext(), isLoading);
    }

    /**
     * @自定义loading样式的请求并且请求带错误回调
     */
    protected ComObserver request(OnLoadingListener onLoadingListener, AlertDialog alertDialog, ProgressManageError progressManageError) {
        return new ComObserver(mCompositeDisposable, onLoadingListener, alertDialog, progressManageError, getContext(), true);
    }
}
